setwd("/media/datos/ceu/docencia/materiales/estadistica/presentaciones/curso_estadistica")

#Regiones de aceptación y de rechazo de un contraste bilateral
x11()
x = seq(-3.291, 3.291, length=100) 
y = dnorm(x, mean=0, sd=1)
par(cex.lab=1.2)
plot(x, y, type="l", ylim=c(-0.02,0.4), main="Distribución del estadístico del contraste", xlab=expression(hat(theta)), ylab=expression(f(hat(theta))), xaxt="n", yaxt="n") 
axis(side=1,c(x[21],0,x[80]))
axis(side=2,c(0))
polygon(c(x[1],x[1:21],x[21]), c(0,y[1:21],0), col="coral", lty=0)
polygon(c(x[80],x[80:100],x[100]), c(0,y[80:100],0), col="coral", lty=0) 
text(-2.2,0.02,expression(alpha/2))
text(2.2,0.02,expression(alpha/2))
text(0,-0.015,"Aceptación")
text(-2.4,-0.015,"Rechazo")
text(2.4,-0.015,"Rechazo")
abline(h=0,col="gray")
dev.copy2eps(file="img/contrastes/regiones_bilateral.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/contrastes/regiones_bilateral.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(x, y, type="l", ylim=c(-0.02,0.4), main="Distribución del estadístico del contraste", xlab=expression(hat(theta)), ylab=expression(f(hat(theta))), xaxt="n", yaxt="n") 
axis(side=1,c(x[21],0,x[80]))
axis(side=2,c(0))
polygon(c(x[1],x[1:21],x[21]), c(0,y[1:21],0), col="coral", lty=0)
polygon(c(x[80],x[80:100],x[100]), c(0,y[80:100],0), col="coral", lty=0) 
text(-2.2,0.02,expression(alpha/2))
text(2.2,0.02,expression(alpha/2))
text(0,-0.015,"Aceptación")
text(-2.4,-0.015,"Rechazo")
text(2.4,-0.015,"Rechazo")
abline(h=0,col="gray")
dev.off()

#Regiones de aceptación y de rechazo de un contraste bilateral
x11()
x = seq(-3.291, 3.291, length=100) 
y = dnorm(x, mean=0, sd=1)
par(cex.lab=1.2)
plot(x, y, type="l", ylim=c(-0.02,0.4), main="Distribución del estadístico del contraste", xlab=expression(hat(theta)), ylab=expression(f(hat(theta))), xaxt="n", yaxt="n") 
axis(side=1,c(0,x[76]))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0) 
text(2,0.02,expression(alpha))
text(0,-0.015,"Aceptación")
text(2.3,-0.015,"Rechazo")
abline(h=0,col="gray")
dev.copy2eps(file="img/contrastes/regiones_unilateral_mayor.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/contrastes/regiones_unilateral_mayor.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(x, y, type="l", ylim=c(-0.02,0.4), main="Distribución del estadístico del contraste", xlab=expression(hat(theta)), ylab=expression(f(hat(theta))), xaxt="n", yaxt="n") 
axis(side=1,c(0,x[76]))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0) 
text(2,0.02,expression(alpha))
text(0,-0.015,"Aceptación")
text(2.3,-0.015,"Rechazo")
abline(h=0,col="gray")
dev.off()

#Riesgo beta para un diferencia pequeña
x11()
x = seq(-3.291, 3.291, length=100) 
y = dnorm(x, mean=0, sd=1)
par(cex.lab=1.2)
plot(x, y, type="l", xlim=c(-3.3,6.8), ylim=c(-0.02,0.45), main=expression(paste("Relación entre el riesgo ", beta, " y la mínima diferencia importante ", delta)), xlab=expression(hat(theta)), ylab=expression(f(hat(theta))), xaxt="n", yaxt="n")
lines(x+3, y, type="l")
text(0,0.42, expression(H[0]: theta == theta[0]))
text(3,0.42, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[76],3))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0) 
polygon(c(x[1]+3,x[1:31]+3,x[31]+3), c(0,y[1:31],0), col="royalblue1", lty=0)
text(2,0.02, expression(alpha))
text(1.2,0.02, expression(beta))
text(0,-0.015,"Aceptación")
text(3,-0.015,"Rechazo")
abline(h=0,col="gray")
dev.copy2eps(file="img/contrastes/riesgo_diferencia_pequenia.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/contrastes/riesgo_diferencia_pequenia.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(x, y, type="l", xlim=c(-3.3,6.8), ylim=c(-0.02,0.45), main=expression(paste("Relación entre el riesgo ", beta, " y la mínima diferencia importante ", delta)), xlab=expression(hat(theta)), ylab=expression(f(hat(theta))), xaxt="n", yaxt="n")
lines(x+3, y, type="l")
text(0,0.42, expression(H[0]: theta == theta[0]))
text(3,0.42, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[76],3))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0) 
polygon(c(x[1]+3,x[1:31]+3,x[31]+3), c(0,y[1:31],0), col="royalblue1", lty=0)
text(2,0.02, expression(alpha))
text(1.2,0.02, expression(beta))
text(0,-0.015,"Aceptación")
text(3,-0.015,"Rechazo")
abline(h=0,col="gray")
dev.off()

#Riesgo beta para un diferencia grande
x11()
x = seq(-3.291, 3.291, length=100) 
y = dnorm(x, mean=0, sd=1)
par(cex.lab=1.2)
plot(x, y, type="l", xlim=c(-3.3,6.8), ylim=c(-0.02,0.45), main=expression(paste("Relación entre el riesgo ", beta, " y la mínima diferencia importante ", delta)), xlab=expression(hat(theta)), ylab=expression(f(hat(theta))), xaxt="n", yaxt="n")
lines(x+3.46, y, type="l")
text(0,0.42, expression(H[0]: theta == theta[0]))
text(3.46,0.42, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[76],3.46))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0) 
polygon(c(x[1]+3.46,x[1:24]+3.46,x[24]+3.46), c(0,y[1:24],0), col="royalblue1", lty=0)
text(2,0.02, expression(alpha))
text(1.4,0.02, expression(beta))
text(0,-0.015,"Aceptación")
text(3,-0.015,"Rechazo")
abline(h=0,col="gray")
dev.copy2eps(file="img/contrastes/riesgo_diferencia_grande.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/contrastes/riesgo_diferencia_grande.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(x, y, type="l", xlim=c(-3.3,6.8), ylim=c(-0.02,0.45), main=expression(paste("Relación entre el riesgo ", beta, " y la mínima diferencia importante ", delta)), xlab=expression(hat(theta)), ylab=expression(f(hat(theta))), xaxt="n", yaxt="n")
lines(x+3.46, y, type="l")
text(0,0.42, expression(H[0]: theta == theta[0]))
text(3.46,0.42, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[76],3.46))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0) 
polygon(c(x[1]+3.46,x[1:24]+3.46,x[24]+3.46), c(0,y[1:24],0), col="royalblue1", lty=0)
text(2,0.02, expression(alpha))
text(1.4,0.02, expression(beta))
text(0,-0.015,"Aceptación")
text(3,-0.015,"Rechazo")
abline(h=0,col="gray")
dev.off()

#Relación riesgo alpha y beta
x11()
x = seq(-3.291, 3.291, length=100)
y = dnorm(x, mean=0, sd=1)
par(cex.lab=1.2)
plot(x, y, xlim=c(-3.3,6.3), ylim=c(0,0.45), xlab=expression(hat(theta)), ylab=expression(italic(f(hat(theta)))), main=expression(paste("Relación entre ", alpha, " y ", beta)), type="l", xaxt="n", yaxt="n")
lines(x+3, y, type="l")
text(0,0.42, expression(H[0]: theta == theta[0]))
text(3,0.42, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[76],3))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0)
polygon(c(x[1]+3,x[1:31]+3,x[31]+3), c(0,y[1:31],0), col="royalblue1", lty=0)
text(2,0.02, expression(alpha))
text(1.2,0.02, expression(beta))
abline(h=0, col="gray")
dev.copy2eps(file="img/contrastes/relacion_riesgos.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/contrastes/relacion_riesgos.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(x, y, xlim=c(-3.3,6.3), ylim=c(0,0.45), xlab=expression(hat(theta)), ylab=expression(italic(f(hat(theta)))), main=expression(paste("Relación entre ", alpha, " y ", beta)), type="l", xaxt="n", yaxt="n")
lines(x+3, y, type="l")
text(0,0.42, expression(H[0]: theta == theta[0]))
text(3,0.42, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[76],3))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0)
polygon(c(x[1]+3,x[1:31]+3,x[31]+3), c(0,y[1:31],0), col="royalblue1", lty=0)
text(2,0.02, expression(alpha))
text(1.2,0.02, expression(beta))
abline(h=0, col="gray")
dev.off()

#Riesgos tamaño muestral pequeño
x11()
x = seq(-3.291, 3.291, length=100)
y = dnorm(x, mean=0, sd=1)
par(cex.lab=1.2)
plot(x, y, xlim=c(-3.3,6.3), ylim=c(0,0.52), xlab=expression(hat(theta)), ylab=expression(italic(f(hat(theta)))), main="Riesgos de error para muestras pequeñas", type="l", xaxt="n", yaxt="n")
lines(x+3, y, type="l")
text(0,0.51, expression(H[0]: theta == theta[0]))
text(3,0.51, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[76],3))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0)
polygon(c(x[1]+3,x[1:31]+3,x[31]+3), c(0,y[1:31],0), col="royalblue1", lty=0)
text(2,0.02, expression(alpha))
text(1.2,0.02, expression(beta))
abline(h=0, col="gray")
dev.copy2eps(file="img/contrastes/riesgos_muestras_pequeñas.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/contrastes/riesgos_muestras_pequeñas.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(x, y, xlim=c(-3.3,6.3), ylim=c(0,0.52), xlab=expression(hat(theta)), ylab=expression(italic(f(hat(theta)))), main="Riesgos de error para muestras pequeñas", type="l", xaxt="n", yaxt="n")
lines(x+3, y, type="l")
text(0,0.51, expression(H[0]: theta == theta[0]))
text(3,0.51, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[76],3))
axis(side=2,c(0))
polygon(c(x[76],x[76:100],x[100]), c(0,y[76:100],0), col="coral", lty=0)
polygon(c(x[1]+3,x[1:31]+3,x[31]+3), c(0,y[1:31],0), col="royalblue1", lty=0)
text(2,0.02, expression(alpha))
text(1.2,0.02, expression(beta))
abline(h=0, col="gray")
dev.off()

#Riesgos tamaño muestral grande
x11()
x = seq(-3.291, 3.291, length=100)
y = dnorm(x, mean=0, sd=0.8)
par(cex.lab=1.2)
plot(x, y, xlim=c(-3.3,6.3), ylim=c(0,0.52), xlab=expression(hat(theta)), ylab=expression(italic(f(hat(theta)))), main="Riesgos de error para muestras grandes", type="l", xaxt="n", yaxt="n")
lines(x+3, y, type="l")
text(0,0.51, expression(H[0]: theta == theta[0]))
text(3,0.51, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[73],3))
axis(side=2,c(0))
polygon(c(x[73],x[73:100],x[100]), c(0,y[73:100],0), col="coral", lty=0)
polygon(c(x[1]+3,x[1:28]+3,x[28]+3), c(0,y[1:28],0), col="royalblue1", lty=0)
text(1.8,0.02, expression(alpha))
text(1.2,0.02, expression(beta))
abline(h=0, col="gray")
dev.copy2eps(file="img/contrastes/riesgos_muestras_grandes.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/contrastes/riesgos_muestras_grandes.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(x, y, xlim=c(-3.3,6.3), ylim=c(0,0.52), xlab=expression(hat(theta)), ylab=expression(italic(f(hat(theta)))), main="Riesgos de error para muestras grandes", type="l", xaxt="n", yaxt="n")
lines(x+3, y, type="l")
text(0,0.51, expression(H[0]: theta == theta[0]))
text(3,0.51, expression(H[1]: theta == theta[0]+delta))
axis(side=1,c(0,x[73],3))
axis(side=2,c(0))
polygon(c(x[73],x[73:100],x[100]), c(0,y[73:100],0), col="coral", lty=0)
polygon(c(x[1]+3,x[1:28]+3,x[28]+3), c(0,y[1:28],0), col="royalblue1", lty=0)
text(1.8,0.02, expression(alpha))
text(1.2,0.02, expression(beta))
abline(h=0, col="gray")
dev.off()

#Curva de potencia
x11()
x = seq(0,1, length=100)
y = pbinom(1, size=10, prob=x[1:100])
z = pbinom(41, size=100, prob=x[1:100])
par(cex.lab=1.2)
plot(x, y, type="l", xlab="Proporción verdadera", ylab="Potencia", main=expression(paste("Curvas de potencia para ", alpha == 0.05)), col="coral")
lines(x, z, type="l", col="royalblue1")
legend(0.8, 1, lty=1, col=c("coral","royalblue1"), legend=expression(n==10,n==100), bty="n") 
abline(h=0, col="gray")
dev.copy2eps(file="img/contrastes/potencia.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/contrastes/potencia.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(x, y, type="l", xlab="Proporción verdadera", ylab="Potencia", main=expression(paste("Curvas de potencia para ", alpha == 0.05)), col="coral")
lines(x, z, type="l", col="royalblue1")
legend(0.8, 1, lty=1, col=c("coral","royalblue1"), legend=expression(n==10,n==100), bty="n") 
abline(h=0, col="gray")
dev.off()

